/*
 * @lc app=leetcode.cn id=543 lang=javascript
 *
 * [543] 二叉树的直径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var diameterOfBinaryTree = function(root) {
  if(!root)return 0
  let res=0
  let dfs=function(node){
      if(!node)return 0
      let l=dfs(node.left)
      let r=dfs(node.right)
      res=Math.max(l+r,res)
      return Math.max(l,r)+1
  }
  dfs(root)
  return res
};
// @lc code=end

